<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="css/index.css">
    <script src="js/vue.min.js"></script>
    <script src="js/elementui.js"></script>
    <script src="js/axios.min.js"></script>
    <script src="js/common.js"></script>
    <title>Document</title>
</head>

<body>
    <div id="app">
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="uname" label="用户名" width="180">
            </el-table-column>
            <el-table-column label="密码" width="180">
                <template slot-scope="scope">
                    ******
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot="header" slot-scope="scope">
                    <el-button size="mini" type="primary" @click="add">增加</el-button>
                </template>
                <template slot-scope="scope">
                    <el-button size="mini" @click="upuser(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="warning" size="mini" @click="upuserrole(scope.$index, scope.row)">用户角色</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-drawer title="用户角色维护" :visible.sync="drawer">

            &nbsp;&nbsp;&nbsp;
            当前用户角色：{{nowuserid}}<br />

            <template v-for="item in userRoleList">
                &nbsp;
                <el-button @click="deleteUserRole(item.id)" style="margin-bottom: 5px;"  size="small" type="primary" plain>{{item.description}}<i class="el-icon-minus"></i></el-button>
            </template>
            <hr>
            &nbsp;
            <!-- {{roleList}} -->

            <template v-for="item in roleList">
                <el-button style="margin-bottom: 5px;" size="medium" type="primary">
                    {{item.description}}
                    <i class="el-icon-plus"></i>
                </el-button>

                <!-- <el-tag size="medium" closable :type="item.type" style="margin-left: 5px;"
                    effect="dark">{{item.description}}</el-tag> -->
            </template>
        </el-drawer>
    </div>
</body>
<script>
    new Vue({
        el: '#app',
        data: {
            tableData: [],
            drawer: false,//控制用户角色维护抽屉的显示关闭的
            nowuserid: 0,
            userRoleList: [],//当前用户拥有的角色集合
            roleList: [],//所有的角色集合
        },
        methods: {
            initTableData() {
                let that = this
                axios.get(API + "selectuser").then(function (res) {
                    that.tableData = res.data.data.userList
                }).catch(err => {
                    console.log(err)
                });
            },
            add() {

            },
            upuser() {

            },
            upuserrole(i, row) {//修改用户角色
                this.nowuserid = row.id//设置用户id为当前行点击的用户的id 
                // alert(this.nowuserid)
                let that = this
                axios.get(API + "selectRoleByUserId/" + this.nowuserid).
                    then(function (res) {
                        that.userRoleList = res.data.data.roleList
                    }).catch(err => {
                        console.log(err)
                    });

                this.drawer = true;
            },
            allRole() {//获取所有角色
                let that = this
                axios.get(API + "selectRole").then(function (res) {
                    that.roleList = res.data.data.roleList
                }).catch(err => {
                    console.log(err)
                });
            },
            deleteUserRole(roleid) {//根据用户id删除一个角色
                let that = this
                axios.post(API + "deleteByUseridAndRoleid/",{
                    "userid":this.nowuserid,
                    "roleid":roleid
                }).then(function (res) {
                    that.userRoleList = that.userRoleList.filter(function(obj){
                    return obj.id != roleid
                });
                }).catch(err => {
                    console.log(err)
                });
                // alert(this.nowuserid + "===" + roleid)
            },
        },
        mounted() {
            this.initTableData();
            this.allRole();
        }
    })
</script>

</html>